﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication1.DTO;
using System.Data.SqlClient;

namespace WindowsFormsApplication1.DAO
{
    class GiaoVien_CongViecDAO
    {
        public static bool Insert(GiaoVien_CongViecDTO cv)
        {
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;
            cmd.CommandText = @"INSERT INTO GIAOVIEN_CONGVIEC(MaGiaoVien, NamHoc, HocKy, LoaiCongViec, LanThi, SoLuongThucHien) VALUES ('" + cv.maGiaoVien + "'," + cv.namHoc + "," + cv.hocKy + "," + cv.loaiCongViec + "," + cv.lanThi + "," + cv.soLuongThucHien+")";
            if (cmd.ExecuteNonQuery() != -1)
                return true;
            return false;
        }
        /*
        public static List<GiaoVien_CongViecDTO> GetList(string maGiaoVien, int idNamHoc, int idHocKy)
        {
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;
            cmd.CommandText = @"SELECT * FROM GIAOVIEN_CONGVIEC WHERE MaGiaoVien='"+maGiaoVien+"' AND NamHoc="+idNamHoc+" AND HocKy="+idHocKy;

            List<GiaoVien_CongViecDTO> ds = new List<GiaoVien_CongViecDTO>();
            SqlDataReader boDoc = cmd.ExecuteReader();
            while (boDoc.Read())
            {
                GiaoVien_CongViecDTO cv = new GiaoVien_CongViecDTO();

                cv.loaiCongViec = boDoc.GetInt32(4);
                cv.lanThi = boDoc.GetInt32(5);
                cv.soLuongThucHien = boDoc.GetInt32(6);

                ds.Add(cv);
            }
            boDoc.Close();

            return ds;
        }
        */
        internal static int SoDeDaRa(string maGiaoVien, int idNamHoc, int idHocKy)
        {
            return GetSoLuong(maGiaoVien,idNamHoc,idHocKy,Def.CV_RADE);
        }

        internal static int SoBaiDaCham(string maGiaoVien, int idNamHoc, int idHocKy)
        {
            return GetSoLuong(maGiaoVien, idNamHoc, idHocKy, Def.CV_CHAM_TIEULUAN_DOAN) + GetSoLuong(maGiaoVien, idNamHoc, idHocKy, Def.CV_CHAM_TRACNGHIEM_VANDAP);
        }

        internal static int GetSoLuong(string maGiaoVien, int idNamHoc, int idHocKy, int loaiCongViec)
        {
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;
            cmd.CommandText = @"SELECT SoLuongThucHien, LanThi FROM GIAOVIEN_CONGVIEC WHERE MaGiaoVien='" + maGiaoVien + "' AND NamHoc=" + idNamHoc + " AND HocKy=" + idHocKy +" AND LoaiCongViec="+loaiCongViec;

            int tong=0, soluong, lanthi;
            int loaiGiaoVien = GiaoVienDAO.GetMaLoaiGiaoVien(maGiaoVien);
            SqlDataReader boDoc = cmd.ExecuteReader();
            while (boDoc.Read())
            {
                soluong = boDoc.GetInt32(0);
                lanthi = boDoc.GetInt32(1);
                
                bool bTinhCong = true;
                if (loaiGiaoVien == Def.GV_MOIGIANG)
                    if (lanthi == 1)
                        if (loaiCongViec == Def.CV_RADE || loaiCongViec == Def.CV_CHAM_TRACNGHIEM_VANDAP || loaiCongViec == Def.CV_CHAM_TIEULUAN_DOAN || loaiCongViec == Def.CV_CHAM_TULUAN_THIMAY)
                            bTinhCong = false;

                if (bTinhCong)
                {
                    //float tietCong = LoaiCongViecDAO.GetTietCong(loaiCongViec, soluong, lanthi);
                    if (loaiCongViec == Def.CV_TOCHUCPHONGTHI)
                        tong++;
                    else
                        tong += soluong;
                    
                }
            }
            boDoc.Close();
            return tong;
        }





        internal static void ChamCong(ref ChamCongDTO info, int idNamHoc, int idHocKy, int loaiCongViec)
        {
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;
            cmd.CommandText = @"SELECT SoLuongThucHien, LanThi FROM GIAOVIEN_CONGVIEC WHERE MaGiaoVien='" + info.maGiaoVien + "' AND NamHoc=" + idNamHoc + " AND HocKy=" + idHocKy + " AND LoaiCongViec=" + loaiCongViec;

            int tongSoLuong = 0, soluong, lanthi;
            float tongTietCong = 0;
            int loaiGiaoVien = GiaoVienDAO.GetMaLoaiGiaoVien(info.maGiaoVien);
            SqlDataReader boDoc = cmd.ExecuteReader();
            bool bTinhCong = true;
            while (boDoc.Read())
            {
                soluong = boDoc.GetInt32(0);
                lanthi = boDoc.GetInt32(1);

                if (loaiGiaoVien == Def.GV_MOIGIANG)
                    if (lanthi == 1)
                        if (loaiCongViec == Def.CV_RADE || loaiCongViec == Def.CV_CHAM_TRACNGHIEM_VANDAP || loaiCongViec == Def.CV_CHAM_TIEULUAN_DOAN || loaiCongViec == Def.CV_CHAM_TULUAN_THIMAY)
                            bTinhCong = false;

                if (bTinhCong)
                {
                    tongTietCong += LoaiCongViecDAO.GetTietCong(loaiCongViec, soluong, lanthi);
                    if (loaiCongViec == Def.CV_TOCHUCPHONGTHI)
                        tongSoLuong++;
                    else
                        tongSoLuong += soluong;
                }
            }
            boDoc.Close();

            if (bTinhCong)
            {
                switch (loaiCongViec)
                {
                    case Def.CV_NHAPDIEMTHI:
                        break;
                    case Def.CV_RADE:
                        info.congRaDe += tongTietCong;
                        info.deDaRa += tongSoLuong;
                        break;
                    case Def.CV_TOCHUCPHONGTHI:
                        info.toChuc += tongSoLuong;
                        info.congToChuc += tongTietCong;
                        break;
                    case Def.CV_CHAM_TIEULUAN_DOAN:
                    case Def.CV_CHAM_TRACNGHIEM_VANDAP:
                    case Def.CV_CHAM_TULUAN_THIMAY:
                        info.baiDaCham += tongSoLuong;
                        info.congChamBai += tongTietCong;
                        break;
                }
            }
        }
    }
}
